package com.umeox.watch.moto.poweroff;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

/**
 * Created by zl on 2016/8/15.
 */
public class Utils {

    public static final String ACTION_REQUEST_SHUTDOWN = "android.intent.action.ACTION_REQUEST_SHUTDOWN";
    public static final String ACTION_REQUEST_SHUTDOWN_VERSION_CODE_O = "com.android.internal.intent.action.REQUEST_SHUTDOWN";
    public static final String EXTRA_KEY_CONFIRM = "android.intent.extra.KEY_CONFIRM";


    private static ConnectivityManager mCM;

    public static boolean getGprsStatus(Context context) {
        if (mCM == null) {
            mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
        boolean isGprsConnected = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ? true : false;

        return isGprsConnected;

    }


    public static void toggleGprs(Context context, boolean isEnable) {
        try {
            if (mCM == null) {
                mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            }
            Class<?> cmClass = mCM.getClass();
            Class<?>[] argClasses = new Class[1];
            argClasses[0] = boolean.class;

            // 反射ConnectivityManager中hide的方法setMobileDataEnabled，可以开启和关闭GPRS网络
            Method method;
            method = cmClass.getMethod("setMobileDataEnabled", argClasses);
            method.invoke(mCM, isEnable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 设置数据流量开关
     *
     * @param enable
     * @param context
     * @throws Exception
     */
    @SuppressLint("NewApi")
    public static void setDataEnabled(Context context, boolean enable) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            @SuppressLint("PrivateApi")
            Method setDataEnabled = connectivityManager.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
            if (null != setDataEnabled) {
                setDataEnabled.invoke(connectivityManager, enable);
                Log.e("SystemUtils", "setDataEnabled suc");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("SystemUtils", "setDataEnabled exception");
        }
    }


    /**
     * 获取数据流量开关 需要系统权限
     *
     * @param context
     * @return
     * @throws Exception
     */
    @SuppressLint("NewApi")
    public static boolean getDataEnabled(Context context) {
        boolean enabled = false;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            @SuppressLint("PrivateApi")
            Method getDataEnabled = connectivityManager.getClass().getDeclaredMethod("getMobileDataEnabled");
            if (null != getDataEnabled) {
                enabled = (Boolean) getDataEnabled.invoke(connectivityManager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return enabled;
    }

    public static void shutdown(Context context){
        Intent shutdown = new Intent(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? ACTION_REQUEST_SHUTDOWN_VERSION_CODE_O : ACTION_REQUEST_SHUTDOWN);
        shutdown.putExtra(EXTRA_KEY_CONFIRM, false);
        shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(shutdown);
    }
}
